//
//  OauthViewController.swift
//  NBblock
//
//  Created by apple on 15/9/25.
//  Copyright © 2015年 cc. All rights reserved.
//

import UIKit

class OauthViewController: UIViewController, UIWebViewDelegate {
    
    // 创建并加载webView
    private lazy var webView = UIWebView()
    
    override func loadView() {
        view = webView
        webView.delegate = self
        title = "微博授权登录"
        
        // 设置导航按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: UIBarButtonItemStyle.Plain, target: self, action: "closeMethod")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFill")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

       webView.loadRequest(NSURLRequest(URL: NetworkingTools.sharedTools.oauthURL))
    }
    
    // MARK: -按钮监听方法
    @objc private func closeMethod() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFill() {
        
        let js = "document.getElementById('userId').value = '261983659@qq.com';"+"document.getElementById('passwd').value = 'cs234551';"
        
        webView.stringByEvaluatingJavaScriptFromString(js)
        
    }
    
    // MARK: - UIWebViewDelegate 代理方法
    // 是否加载当前请求
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        return true
    }
    // 加载失败
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        
    }
    
    // 开始加载
    func webViewDidStartLoad(webView: UIWebView) {
        
    }
    
    // 完成加载
    func webViewDidFinishLoad(webView: UIWebView) {
        
    }

}